package itsm.isperp.module.controller.task;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.task.TskInspectCiPlan;
import itsm.isperp.module.service.configuration.ConfCiService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.task.TskInspectCiPlanService;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2015-05-09 22:48
 */
@Controller
@RequestMapping("task/inspectCiPlan")
public class TskInspectCiPlanController extends
		BaseController<TskInspectCiPlan> {

	@Autowired
	protected TskInspectCiPlanService tskInsecptCiPlanService;

	@Autowired
	protected ConfCiService confCiService;

	@Override
	public TskInspectCiPlanService getService() {
		return this.tskInsecptCiPlanService;
	}

	@Override
	public String getPageTitle() {
		return "巡检任务";
	}

	@RequestMapping("saveWithCi")
	@ResponseBody
	public ResultMessage saveWithCi(String ciId, String relationId,
			HttpServletRequest req) {
		if (StringUtils.isEmpty(relationId)) {
			return ResultMessage.failure("关联id不能为空！");
		}
		String[] ciIds = ciId.split(",");
		for (String cId : ciIds) {
			TskInspectCiPlan model = new TskInspectCiPlan();
			model.setCiId(cId);
			model.setPlanId(relationId);
			this.getService().saveOrUpdate(model);
		}

		return ResultMessage.success();
	}

	@RequestMapping("listCi")
	@ResponseBody
	public PageResponse<Map<String, Object>> listCi(JqGridRequest request,
			HttpServletRequest req) {
		String relationId = req.getParameter("relationId");

		String projectId = req.getParameter("projectId");

		ProjProjectService projProjectService = ContextHolder
				.getSpringBean("projProjectService");
		Map<String, Object> map = projProjectService.findOneToMap(projectId,
				"cus_customer_id");

		request.addFilterRule("cus_customer_id", JqGridFilterRuleOp.eq, map
				.get("cus_customer_id").toString());

		request.setSearchString(" a.id NOT IN (SELECT ci_id FROM tsk_inspect_ci_plan WHERE plan_id='"
				+ relationId + "')");

		return this.confCiService.findAllDetailByRequest(request).toPage();

	}

	@RequestMapping("listDetail")
	@ResponseBody
	@Override
	public PageResponse<Map<String, Object>> listDetail(JqGridRequest request,
			HttpServletRequest req) {

		String relationId = req.getParameter("relationId");
		if (StringUtils.isNotEmpty(relationId)) {
			request.addFilterRule("plan_id", JqGridFilterRuleOp.eq, relationId);
		}

		return this.getService().findAllDetailByRequest(request).toPage();

	}

}
